package com.tiejun.ge.http.controller;

import com.tiejun.ge.common.core.domain.R;
import com.tiejun.ge.http.service.FeignService;
import com.tiejun.ge.http.service.HttpService;
import com.tiejun.ge.http.service.RestTemplateService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/**
 * @program: springboot
 * @description: HttpController
 * @author: getiejun
 * @create: 2025-08-14 23:56
 **/
@RestController
public class HttpController {

    @Resource
    private HttpService httpService;

    @Resource
    private RestTemplateService restTemplateService;

    @Resource
    private FeignService feignService;

    @GetMapping("/http/helloWorld")
    public R<?> httpHelloWorld(){
        httpService.getHelloWorld();
        return R.success();
    }

    @GetMapping("/http/getUserInfo")
    public R<?> httpgGetUserInfo(){
        httpService.getUserInfo();
        return R.success();
    }

    @GetMapping("/rest/helloWorld")
    public R<?> restHelloWorld(){
        restTemplateService.getHelloWorld();
        return R.success();
    }

    @GetMapping("/rest/getUserInfo")
    public R<?> restGetUserInfo(){
        restTemplateService.getUserInfo();
        return R.success();
    }


    @GetMapping("/feign/helloWorld")
    public R<?> feignHelloWorld(){
        return feignService.getHelloWorld();
    }

    @GetMapping("/feign/getUserInfo")
    public R<?> feignGetUserInfo(){
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("id", 1L);
        return feignService.getUserInfo(param);
    }

    @GetMapping("/feign/thread/helloWorld")
    public R<?> threadHelloWorld() throws InterruptedException {
        CountDownLatch downLatch = new CountDownLatch(40);
        ExecutorService executorService = Executors.newFixedThreadPool(40);

        IntStream.range(0, 40).forEach(i -> {
            executorService.submit(() -> {
                feignService.getHelloWorld();
            });
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            downLatch.countDown();
        });

        downLatch.await();
        executorService.shutdown();

        return R.success();
    }


}
